package guda.house.web.action.sys.village.building.unit.suite;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.SuiteBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.form.village.SuiteEditForm;
import guda.house.biz.form.village.SuiteForm;
import guda.house.biz.query.SuiteQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.BuildingDO;
import guda.house.dal.dataobject.SuiteDO;
import guda.house.dal.dataobject.UnitDO;
import guda.house.dal.dataobject.VillageDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * Created by well on 15/11/13.
 */
@Controller
public class SuiteAction {

    @Autowired
    private SuiteBiz suiteBiz;

    @RequestMapping(value = "sys/village/building/unit/suite/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        long unitId = RequestUtil.getInt(request, "unitId");
        if(unitId == 0){
            return "redirect:/error.htm?code="+ CommonResultCode.PARAM_MISS;
        }
        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setUnitId(unitId);
        suiteQuery.setPageNo(pageId);
        suiteQuery.setPageSize(pageSize);
        modelMap.put("query",suiteQuery);
        BizResult bizResult = suiteBiz.list(suiteQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            //UnitDO unitDO = (UnitDO)bizResult.data.get("unitDO");
            suiteQuery.setBuildingId(buildingDO.getBuildingId());
            suiteQuery.setVillageId(buildingDO.getVillageId());
            return "sys/village/building/unit/suite/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "sys/village/building/unit/suite/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, SuiteEditForm suiteEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = suiteBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            SuiteDO suiteDO =  (SuiteDO) bizResult.data.get("suiteDO");
            suiteEditForm.initForm(suiteDO);

            UnitDO unitDO = (UnitDO)bizResult.data.get("unitDO");
            suiteEditForm.setUnitId(unitDO.getUnitId());
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            suiteEditForm.setBuildingId(buildingDO.getBuildingId());
            suiteEditForm.setVillageId(buildingDO.getVillageId());

            bizResult = suiteBiz.queryForInit(suiteDO.getUnitId());
            modelMap.putAll(bizResult.data);
            return "sys/village/building/unit/suite/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/suite/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = suiteBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/village/building/unit/suite/detail.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/suite/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, SuiteForm suiteForm,
                         BindingResult result) {
        long unitId = RequestUtil.getInt(request, "unitId");
        if(unitId == 0){
            return "redirect:/error.htm?code="+ CommonResultCode.PARAM_MISS;
        }
        BizResult bizResult = suiteBiz.queryForInit(unitId);
        BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
        suiteForm.setVillageId(buildingDO.getVillageId());
        suiteForm.setBuildingId(buildingDO.getBuildingId());
        UnitDO unitDO = (UnitDO)bizResult.data.get("unitDO");
        suiteForm.setUnitId(unitDO.getUnitId());

        //房号交付时间默认设置为小区交付时间，用来统计物业费
        VillageDO villageDO = (VillageDO)bizResult.data.get("villageDO");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        suiteForm.setGmtDelivery(simpleDateFormat.format(villageDO.getGmtDelivery()));
        modelMap.putAll(bizResult.data);
        return "sys/village/building/unit/suite/create.vm";
    }

    @RequestMapping(value = "sys/village/building/unit/suite/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid SuiteForm suiteForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = suiteBiz.queryForInit(suiteForm.getUnitId());
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            suiteForm.setVillageId(buildingDO.getVillageId());
            suiteForm.setBuildingId(buildingDO.getBuildingId());
            UnitDO unitDO = (UnitDO)bizResult.data.get("unitDO");
            suiteForm.setUnitId(unitDO.getUnitId());
            modelMap.putAll(bizResult.data);
            return "sys/village/building/unit/suite/create.vm";
        }
        SuiteDO suiteDO = suiteForm.toDO();
        BizResult bizResult = suiteBiz.create(suiteDO);
        if (bizResult.success) {
            return "redirect:/sys/village/building/unit/suite/list.htm?unitId="+suiteDO.getUnitId();
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/suite/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid SuiteEditForm suiteEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            BizResult bizResult = suiteBiz.queryForInit(suiteEditForm.getUnitId());
            BuildingDO buildingDO = (BuildingDO)bizResult.data.get("buildingDO");
            suiteEditForm.setVillageId(buildingDO.getVillageId());
            suiteEditForm.setBuildingId(buildingDO.getBuildingId());
            UnitDO unitDO = (UnitDO)bizResult.data.get("unitDO");
            suiteEditForm.setUnitId(unitDO.getUnitId());
            modelMap.putAll(bizResult.data);
            return "sys/village/building/unit/suite/edit.vm";
        }
        SuiteDO suiteDO = suiteEditForm.toDO();
        BizResult bizResult = suiteBiz.update(suiteDO);
        if (bizResult.success) {
            return "redirect:/sys/village/building/unit/suite/list.htm?unitId="+suiteDO.getUnitId();
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/village/building/unit/suite/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = suiteBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
